from django.http import HttpResponse
from django.core.serializers import serialize

from ...models.chat_menu import ChatMenu

import json


def menu_list(request):
    all_chatmenu_obj = ChatMenu.objects.filter(enabled=True).order_by('sort_num')

    json_data_str = serialize('json', all_chatmenu_obj)
    json_data = json.loads(json_data_str)

    data = []
    for i in range(len(json_data)):
        json_data[i]['fields']['id'] = json_data[i]['pk']

        data.append(json_data[i]['fields'])

    res_mes = {
        "code": 0,
        "data": data
    }
    res_mes_json = json.dumps(res_mes, ensure_ascii=False)
    return HttpResponse(res_mes_json)

